package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:34
 * @ Description: LogintableDaoImpl
 **/

import com.hy.bean.Logintable;
import com.hy.dao.LogintableDao;
import com.hy.exception.BizException;
import com.hy.utils.DBManager;
import com.hy.vo.LogintableEmpVo;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class LogintableDaoImpl implements LogintableDao {

    private final QueryRunner queryRunner = new QueryRunner();

    BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());

    @Override
    public Logintable queryById(Integer id) {
        return null;
    }

    @Override
    public Logintable queryByEmpId(Integer empId) {
        Connection connection = null;
        String sql = "select * from logintable where employeeTable_id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(Logintable.class), empId);
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    /* @Description: 根据用户名查询
    * @Date: 2024/5/14 23:29
    * @paramType: String
    * @Param: loginname
    * @Return: Logintable
    */
    @Override
    public LogintableEmpVo queryByLoginname(String loginname) {
        Connection connection = null;
        String sql = "select log.*,emp.duty from logintable log left join employeetable emp on log.employeeTable_id = emp.id where loginName = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(LogintableEmpVo.class,basicRowProcessor), loginname);
        }catch (Exception e){
            throw new BizException("数据库连接异常",e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public List<Logintable> queryAllByLimit(Integer currPage, Integer pageSize) {
        Connection connection = null;
        String sql = "select log.id,log.loginName from logintable log join employeetable emp on log.employeeTable_id = emp.id where emp.duty != '系统管理员' limit ?,?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanListHandler<>(Logintable.class,basicRowProcessor), (currPage-1)*pageSize, pageSize);
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public long count() {
        Connection connection = null;
        String sql = "select count(*) count from logintable log join employeetable emp on log.employeeTable_id = emp.id where emp.duty != '系统管理员'";
        try {
            connection = DBManager.getConnection();
            return (long) queryRunner.query(connection, sql, new ScalarHandler<>("count"));
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int insert(Logintable logintable) {
        Connection connection = null;
        String sql = "insert into logintable(id,loginName,password,employeeTable_id) values(null,?,?,?)";
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection, sql, logintable.getLoginName(), logintable.getPassword(), logintable.getEmployeetableId());
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int update(Logintable logintable) {
        return 0;
    }

    @Override
    public int updatePasswordById(Logintable logintable){
        Connection connection = null;
        String sql = "update logintable set password = ? where id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection, sql, logintable.getPassword(), logintable.getId());
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int deleteById(Integer id) {
        Connection connection = null;
        String sql = "delete from logintable where id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection, sql, id);
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public Logintable getLoginByName(String loginName) {
        Connection connection = null;
        String sql = "select * from logintable where loginName = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(Logintable.class,basicRowProcessor),loginName);
        }catch (Exception e){
            throw new BizException("Dao异常");
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
